package com.danco.training.zaneuskaya.faces;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.service.SearchService;
import com.danco.training.zaneuskaya.service.UserService;
import com.danco.training.zaneuskaya.service.exception.UserException;

/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Component
@Scope("session")
public class ProfileBean implements Serializable {

	private static final String USER_LIST = "/user/list";

	private static final String USER_MAIN_FACES_REDIRECT_TRUE = "/user/main?faces-redirect=true";

	private static final String HOME_XHTML_FACES_REDIRECT_TRUE = "/home.xhtml?faces-redirect=true";

	/**
	 * 
	 */
	private static final long serialVersionUID = -4118774947244084614L;

	private static final Logger LOGGER = Logger.getLogger(ProfileBean.class);

	private int userId;

	private String userName;

	private User currentUser;

	private List<User> userList;

	private List<User> userFriends;

	@Autowired
	private UserBean userBean;

	@Autowired
	private UserService userService;

	@Autowired
	private SearchService searchService;

	private boolean selfPageVisitor;

	private boolean friendOfSelf;

	public SearchService getSearchService() {
		return searchService;
	}

	public void setSearchService(SearchService searchService) {
		this.searchService = searchService;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public boolean isFriendOfSelf() {
		if (userBean.getSelf() == null || currentUser == null) {
			return false;
		}
		return userService.friendOf(userBean.getSelf(), currentUser);

	}

	public void setFriendOfSelf(boolean friendOfSelf) {
		this.friendOfSelf = friendOfSelf;
	}

	public boolean isSelfPageVisitor() {
		if (userBean.getSelf() == null || currentUser == null) {

			return false;
		}
		return userBean.getSelf().getUserId() == currentUser.getUserId();

	}

	public void setSelfPageVisitor(boolean selfPageVisitor) {
		this.selfPageVisitor = selfPageVisitor;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public int getUserId() {

		return userId;
	}

	public void setUserId(int userId) {
		currentUser = userService.getEntityById(userId);
		this.userId = userId;
	}

	public List<User> getUserFriends() {
		userFriends = userService.getFriends(currentUser);
		return userFriends;
	}

	public void setUserFriends(List<User> userFriends) {
		this.userFriends = userFriends;
	}

	public User getCurrentUser() {
		return currentUser;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}

	public List<User> getUserList() {
		userList = userService.getEntityList();
		return userList;
	}

	public void setUserList(List<User> userList) {
		this.userList = userList;
	}

	/**
	 * @return logged in user page if user is logged in
	 */
	public String toSelfPage() {
		if (userBean.getSelf() == null) {
			return HOME_XHTML_FACES_REDIRECT_TRUE;
		} else {
			currentUser = userBean.getSelf();
			return USER_MAIN_FACES_REDIRECT_TRUE;
		}

	}

	public String addFriend() {

		if (!isSelfPageVisitor() && !isFriendOfSelf()) {
			try {
				currentUser = userService
						.getEntityById(currentUser.getUserId());
				userService.addFriend(userBean.getSelf(), currentUser);
			} catch (UserException e) {
				LOGGER.error(e.getMessage(), e);

			}
			return USER_LIST;
		}
		return USER_MAIN_FACES_REDIRECT_TRUE;
	}

	/**
	 * Search of users by first name and last name.
	 * 
	 * @param name
	 * @return List of founded users names corresponding @param of search
	 */
	public List<String> completeName(String name) {
		List<String> res = new ArrayList<String>();
		for (User u : searchService.findUserNamesByName(name)) {
			res.add(u.getFirstName() + " " + u.getLastName());
			setCurrentUser(u);
		}
		return res;
	}

}
